﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;


/**
 * @file ModelList holds the list of CatsModel
 * 
 * @author LeonXie
 * */

namespace Catsland.Core {

    /**
     * @brief ModelList holds the list of CatsModel and hold by Scene
     * */
    public class ModelList : UniqueList<CatsModel> {
        public void AddModel(CatsModel model) {
            base.AddItem(model._name, model);

            if (Mgr<GameEngine>.Singleton._gameEngineMode
                == GameEngine.GameEngineMode.MapEditor) {
                Mgr<GameEngine>.Singleton.Editor.UpdateModelList(contentList);
            }

        }

        public override void ReleaseAll() {
            base.ReleaseAll();

            if (Mgr<GameEngine>.Singleton._gameEngineMode
                == GameEngine.GameEngineMode.MapEditor) {
                Mgr<GameEngine>.Singleton.Editor.UpdateModelList(contentList);
            }
        }

        public CatsModel GetModel(String name) {
            return base.GetItem(name);
        }

        public bool SaveToNode(XmlNode node, XmlDocument doc) {
            XmlElement models = doc.CreateElement("Models");
            node.AppendChild(models);

            foreach (KeyValuePair<String, CatsModel> pair in contentList) {
                pair.Value.SaveToNode(models, doc);
            }

            return true;
        }

        public static ModelList LoadFromNode(XmlNode node, Scene scene) {
            ModelList newModelList = new ModelList();

            foreach (XmlNode model in node.ChildNodes) {
                CatsModel newModel = CatsModel.LoadFromNode(model, scene);
                newModelList.AddModel(newModel);
            }

            return newModelList;
        }
    }
}
